<?php
$view = 0;
$page = 1;
if (isset($req['p']))
	$page = $req['p'];
$max_display = 10;
$sort_by = 'ORDER BY iname ASC';
$search = "";
if (COUNT($_POST) > 0)
{
	if ($this->get('view'))
		$view = $this->get('view');
	if ($this->get('display'))
		$max_display = $this->get('display');
	if ($this->get('idkey')){
		$key2 = $this->get('idkey');
		$search = "WHERE id.name='" . $key2 . "' OR id.iid='" . $key2 . "'";
	} else
	if ($this->get('key')){
		$key = $this->get('key');
		$search = "WHERE id.name like '%" . $key . "%'";
	}
	if ($this->get('sort_by')){
		if($this->get('sort_with'))
			$sort_by = " ASC";
		else
			$sort_by = " DESC";
			
		$sort_by = "ORDER BY " . $this->get('sort_by') . $sort_by;
	}
}
if (true){
	$first = ($page-1)*$max_display;
	if ($first <0)
		$first = 0;
	$sql = "SELECT *, id.name as iname, ic.name as cname,iud.name as uname,ii.quantity as qty, DATEDIFF(NOW(), date_enter) as isnew, TIME_TO_SEC(TIMEDIFF(NOW(), date_enter)) as isadded, ((ii.quantity * 100 / (ii.reorder * 4))) as stock, id.description as descript FROM item_inventory ii JOIN item_details id USING(iid) JOIN item_category ic USING(icid) JOIN supplier_item_cost s USING(iid) JOIN item_unit_detail iud USING(imdid) $search GROUP BY ii.iid $sort_by LIMIT $first,$max_display";
	Connection::getConnect();
    Connection::setQuery($sql);
	$list = Connection::fetchAll(MYSQL_ASSOC);
	
	$sql = "SELECT COUNT(id.name)FROM item_inventory ii JOIN item_details id USING(iid)";
    Connection::setQuery($sql);
	$total = Connection::fetch();
}

function getSupplier($iid){
	Connection::getConnect();
	$sql = "SELECT name FROM supplier_item_cost JOIN supplier_details USING(sdid) WHERE iid=$iid";
    Connection::setQuery($sql);
	$list = Connection::fetchAll(MYSQL_ASSOC);
	$lists = "";
	foreach($list as $name){
		if(strrpos($lists, $name['name']) === false)
			$lists .= $name['name'] . ', ';
	}
	return rtrim($lists,", ");
}
?>
